/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package regulatoryNetworks;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import units.CellType;
import units.MonkeyType;
import utils.FileUtils;

/**
 *
 * @author jamie
 */
public class MinetResultsParser {

    private double miThreshold = 0.3;
    private ArrayList<String> probeIds;

    public void setMiThreshold(double miThreshold) {
        this.miThreshold = miThreshold;
    }

    public double getMiThreshold() {
        return miThreshold;
    }
    public Map<String, Double> getResultsFromFile(MonkeyType mt, CellType ct) throws IOException {
        File inputFolder = new File("/Users/jamie/Dropbox/monkeys/outputForR/minet/");
        String source = mt.name() + ct.name();
        File inputFile = new File(inputFolder, source + "_select_results.txt");
        return getResultsFromFile(inputFile);
    }


    public Map<String, Double> getResultsFromFile(File inputFile) throws IOException {
        Map<String, Double> results = new TreeMap<String, Double>();
        BufferedReader reader = FileUtils.getBufferedReader(inputFile);
        String line;
        boolean first = true;
        probeIds = new ArrayList<String>();
        while ((line = reader.readLine()) != null) {
            String[] split = line.split("\t");
            if (first) {
                for (String s : split) {
                    String id = s.substring(1);
                    probeIds.add(id);
                }
                first = false;
            } else {
                String probeId_1 = split[0].substring(1);
                for (int i = 1; i < split.length; i++) {
                    double mi = Double.parseDouble(split[i]);
                    if (mi >= miThreshold) {
                        String probeId_2 = probeIds.get(i - 1);
                        String[] ids = {probeId_1, probeId_2};
                        Arrays.sort(ids);
                        String key = ids[0] + ":" + ids[1];
                        results.put(key, mi);
                    }
                }
            }
        }
        reader.close();
        return results;
    }

    List<String> getProbeIds() {
        return probeIds;
    }

}
